package com.sunny.ihealth.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

/**
 * 时间格式化小工具
 * Created by Sunny  An on 2015/10/12.
 */
public class TimeUtils {

    public static String getDay(long timeInMillis) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(timeInMillis);
        return String.format(Locale.getDefault(), "%d年%d月%d日", calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH) + 1, calendar.get(Calendar.DATE));
    }


    public static String getTime(long time) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("kk:mm", Locale.getDefault());
        return simpleDateFormat.format(new Date(time));
    }

    public static long getCreateTime(int dayOffset) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.clear(Calendar.MINUTE);
        calendar.clear(Calendar.SECOND);
        calendar.clear(Calendar.MILLISECOND);
        calendar.add(Calendar.DATE, dayOffset);
        return calendar.getTimeInMillis();
    }

    public static String getFriendlyTime(long time) {
        long cha = System.currentTimeMillis();
        Calendar nowTime = Calendar.getInstance();
        nowTime.setTimeInMillis(cha);
        Calendar sendTime = Calendar.getInstance();
        sendTime.setTimeInMillis(time);
        cha = cha - time;
        long m = cha / 1000;
        if (m < 60)  //60秒内
        {
            return "刚刚";
        }
        m = m / 60;
        if (m < 60) {
            return m + "分钟前";
        }
        m = m / 60;
        //同一天
        if (sendTime.get(Calendar.YEAR) == nowTime.get(Calendar.YEAR)
                && sendTime.get(Calendar.MONTH) == nowTime.get(Calendar.MONTH)
                && sendTime.get(Calendar.DAY_OF_MONTH) == nowTime.get(Calendar.DAY_OF_MONTH)) {
            if (m <= 12) {
                return m + "小时前";
            } else {
                return String.format(Locale.getDefault(), "昨天 %02d:%02d",
                        sendTime.get(Calendar.HOUR_OF_DAY),
                        sendTime.get(Calendar.MINUTE));
            }
        }
        Calendar temp = (Calendar) sendTime.clone();
        //发送日期加一天
        temp.add(Calendar.DATE, 1);
        //昨天发的
        if (temp.get(Calendar.YEAR) == nowTime.get(Calendar.YEAR)
                && temp.get(Calendar.MONTH) == nowTime.get(Calendar.MONTH)
                && temp.get(Calendar.DAY_OF_MONTH) == nowTime.get(Calendar.DAY_OF_MONTH)) {
            return String.format(Locale.getDefault(), "昨天 %02d:%02d",
                    sendTime.get(Calendar.HOUR_OF_DAY),
                    sendTime.get(Calendar.MINUTE));
        }

        return String.format(Locale.getDefault(), "%d月%d日",
                sendTime.get(Calendar.MONTH) + 1,
                sendTime.get(Calendar.DAY_OF_MONTH));
    }

    public static String getFriendlyTime(String time) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault());
        Date date;
        try {
            date = sdf.parse(time);
        } catch (ParseException e) {
            e.printStackTrace();
            return "";
        }
        return getFriendlyTime(date.getTime());
    }

    /**
     * 分钟转换为x时x分
     */
    public static String getHM(int minutes) {
        int hour = minutes / 60;
        int minute = minutes % 60;
        if (hour == 0) {
            return minute + "分钟";
        } else {
            if (minute == 0) {
                return hour + "小时";
            } else {
                return hour + "时" + minute + "分";
            }
        }
    }





}
